using System;
using System.Collections.Generic;
using UnityEngine;
using QFramework;

// 1.请在菜单 编辑器扩展/Namespace Settings 里设置命名空间
// 2.命名空间更改后，生成代码之后，需要把逻辑代码文件（非 Designer）的命名空间手动更改
namespace Rpg.UI
{
	public partial class EntityStatUI : ViewController
	{
		private ControllerBase controller;
		private CharacterBase characterBase;
		private RectTransform rectTransform;
		private void Awake()
		{
			controller = GetComponentInParent<ControllerBase>();
			rectTransform = GetComponent<RectTransform>();
			//flyTextQueue = new Queue<FlyText>();
			//characterBase = GetComponentInParent<CharacterBase>();
		}

		private void Start()
		{
			characterBase = controller.characterBase;
			gameObject.SetActive(true);
			//初始化事件
			controller.OnFlip += OnFlip;
			characterBase.OnReduceHealth += OnReduceHealth;
			characterBase.OnReduceDefense += OnReduceDefense;
			characterBase.OnFireDamage += OnFireDamage;
			characterBase.OnIceDamage += OnIceDamage;
			characterBase.OnElectricDamage += OnElectricDamage;
			characterBase.OnCrit += OnCrit;
			characterBase.OnDodge += OnDodge;
			characterBase.OnDead += OnDead;
			
			characterBase.OnFireStatusStart += OnFireStatusStart;
			characterBase.OnFireStatusEnd += OnFireStatusEnd;
			characterBase.OnIceStatusStart += OnIceStatusStart;
			characterBase.OnIceStatusEnd += OnIceStatusEnd;
			characterBase.OnElectricStatusStart += OnElectricStatusStart;
			characterBase.OnElectricStatusEnd += OnElectricStatusEnd;
		}

		#region buff状态显示事件

		private void OnFireStatusStart()
		{
			//在ui上显示火焰特效或者火焰buff
			Fire.gameObject.SetActive(true);
		}
		private void OnFireStatusEnd()
		{
			Fire.gameObject.SetActive(false);
		}
		
		private void OnIceStatusStart()
		{
			Ice.gameObject.SetActive(true);
		}
		
		 private void OnIceStatusEnd()
		{
			Ice.gameObject.SetActive(false);
		}

		private void OnElectricStatusStart()
		{
			Lightning.gameObject.SetActive(true);
		}
		
		private void OnElectricStatusEnd()
		{
			Lightning.gameObject.SetActive(false);
		}
		#endregion
		
		#region 数值显示事件
		private void OnReduceHealth(float value)
		{
			HValue.FillAmount(value);
		}

		private void OnReduceDefense(float value)
		{
			DValue.FillAmount(value);
			//TODO:播放格挡音效
		}

		private void CreateFlyText(string text,Color color)
		{
			FlyText flyText = PoolMgr.Instance.GetObj("FlyText").GetComponent<FlyText>();
			flyText.Rotation(Quaternion.identity);
			flyText.Parent(transform);
			flyText.Position(transform.position + Vector3.up * 0.5f);
			flyText.SetText(text,color);
		}
		
		private void OnFireDamage(float value)
		{
			CreateFlyText(TextUtil.GetDecimalStr(value,0),Setting.fireColor);
		}


		private void OnIceDamage(float value)
		{
			CreateFlyText(TextUtil.GetDecimalStr(value,0),Setting.iceColor);
		}
		private void OnElectricDamage(float value)
		{
			CreateFlyText(TextUtil.GetDecimalStr(value,0),Setting.electricColor);
		}
		private void OnCrit(float value)
		{
			CreateFlyText(TextUtil.GetDecimalStr(value,0),Setting.critColor);
		}
		private void OnDodge()
		{
			CreateFlyText("Miss",Color.white);
		}
		private void OnDead()
		{
			gameObject.SetActive(false);
		}

		private void OnFlip()
		{
			rectTransform.Rotate(0,180,0);
		}
		#endregion
		private void OnDisable()
		{
			
			controller.OnFlip -= OnFlip;
			characterBase.OnReduceHealth -= OnReduceHealth;
			characterBase.OnReduceDefense -= OnReduceDefense;
			characterBase.OnFireDamage -= OnFireDamage;
			characterBase.OnIceDamage -= OnIceDamage;
			characterBase.OnElectricDamage -= OnElectricDamage;
			characterBase.OnCrit -= OnCrit;
			characterBase.OnDodge -= OnDodge;
			characterBase.OnDead -= OnDead;
			
			characterBase.OnFireStatusStart -= OnFireStatusStart;
			characterBase.OnFireStatusEnd -= OnFireStatusEnd;
			characterBase.OnIceStatusStart -= OnIceStatusStart;
			characterBase.OnIceStatusEnd -= OnIceStatusEnd;
			characterBase.OnElectricStatusStart -= OnElectricStatusStart;
			characterBase.OnElectricStatusEnd -= OnElectricStatusEnd;
			
		}
	}
}
